// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var databind = require("databind");
	var enterpriseId;
	var customerName;
	var tools = require("tools");

	var init = function() {
		enterpriseId = base.getUrlParam("enterpriseId");
		customerName = base.getUrlParam("customerName");
		initEvent();
	};
	var initEvent = function() {
		inquireRankingDatas(enterpriseId, null);
		inquireEnterpriseFinanceSummaryPageList();
		inquireEnterpriseDetail();
	};

	var inquireRankingDatas = function(enterpriseId, bulkstockInfoId) {
		base.fnAjax({
			action: "inquireRankingDatas.json",
			async: true,
			nocode: true,
			oData: {
				type: 4,
				bulkstockInfoId: bulkstockInfoId,
				enterpriseId: enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					data.x = [];
					data.y = [];
					$.each(data.datas, function() {
						var _this = this;
						data.x.push(_this.name);
						data.y.push(_this.value);
					});
//					showChart1(data);
					var chart = echarts.init(document.getElementById("chart"));
					var option = {
						title: {
							text: '客户货品排行',
							subtext: decodeURI(customerName),
							x: 'center',
							left: 'center',
							textStyle: {
								color: "#566bfe",
								fontWeight: 'normal',
							},
							subtextStyle:{
								color: "#566bfe",
								fontSize:'14',
							}
						},
						color: ['#e43b3b'],
						tooltip: {
							trigger: 'axis',
							axisPointer: { // 坐标轴指示器，坐标轴触发有效
								type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
							}
						},
						grid: {
							left: '3%',
							right: '4%',
							bottom: '3%',
							containLabel: true
						},
						xAxis: [{
							type: 'category',
							data: data.x,
							axisTick: {
								alignWithLabel: true
							}
						}],
						yAxis: [{
							type: 'value'
						}],
						textStyle: {
							color: "#fff"
						},
						series: [{
							name: '直接访问',
							type: 'bar',
							barWidth: '60%',
							data: data.y
						}]
					};
					chart.setOption(option, true);
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var inquireEnterpriseFinanceSummaryPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseFinanceSummaryPageList.json",
			async: true,
			nocode: true,
			oData: {
				pageSize: 1000,
				enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					var tpl = $(".summaryTpl").html();
					var html = juicer(tpl, data);
					$(".summaryBody .div1").html(html);
					
					$(".last").unbind("click").click(function(){
						if ($(".hidden").css('display') == 'none') {
							$(".hidden").show();
						} else {
							$(".hidden").hide();
						}
					});
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireEnterpriseDetail.json",
			async: true,
			nocode: true,
			oData: {
				enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					databind.fillForm($(".div2"), data.datas[0]);
					data.datas[0].enterpriseGradeDetail.creditLineRate = data.datas[0].enterpriseGradeDetail.creditAlready*100 / data.datas[0].enterpriseGradeDetail.creditLine;
					data.datas[0].enterpriseGradeDetail.creditLineRemain = data.datas[0].enterpriseGradeDetail.creditLine * 1 - data.datas[0].enterpriseGradeDetail.creditAlready * 1;
					$(".data[name=creditLineRemain]").html(data.datas[0].enterpriseGradeDetail.creditLineRemain);
					$(".data[name=creditLineRate]").html(data.datas[0].enterpriseGradeDetail.creditLineRate);
					databind.fillForm($(".gradeDetail"), data.datas[0].enterpriseGradeDetail.creditAlready);
					
					if(data.datas[0].enterpriseType == '1') {
						$(".data[name=enterpriseType]").html("分销商");
					} else {
						$(".data[name=enterpriseType]").html("直客");
					}
					
					$(".data[name=creditLine]").html(tools.formatCurrency($(".data[name=creditLine]").html()));
					$(".data[name=creditAlready]").html(tools.formatCurrency($(".data[name=creditAlready]").html()));
					$(".data[name=creditLineRemain]").html(tools.formatCurrency($(".data[name=creditLineRemain]").html()));
					
					$(".pledgeRate").html($("td:last").html());
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var showChart1 = function(d) {
		// 基于准备好的dom，初始化echarts实例
		var myChart3 = echarts.init(document.getElementById('main4'));
		//		var localurl = location.href;
		//
		//		var r = getParameter("customerName", localurl);
		//		r = decodeURI(r);
		// alert("====="+r); 
		var vessel = decodeURI(customerName);

		//		var r = getParameter("customerNum", localurl);
		//		r = decodeURI(r);
		// alert("====="+r); 
		//		var customerNum = r.substring(r.indexOf("=") + 1, r.length);
		//alert("货名:"+encodeURIComponent(vessel));

		var option = {
			color: ['#3398DB'],
			title: {
				text: '客户货品放款排行-' + vessel,
				subtext: '一润供应链'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['放款']
			},
			tooltip: {
				trigger: 'axis',
				axisPointer: { // 坐标轴指示器，坐标轴触发有效
					type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
				}
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			toolbox: {
				show: true,
				feature: {
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['line', 'bar']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			calculable: true,
			//			xAxis: [{
			//				type: 'category',
			//				data: d.x.slice(0, 10),
			//				axisTick: {
			//					alignWithLabel: true
			//				}
			//			}],
			//			yAxis: [{
			//				type: 'value'
			//			}],
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: d.x.slice(0, 10),
				show: true,
				axisLabel: {
					interval: 0,
					formatter: function(val) {
						return val.split("").join("\n");
					}
				}
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '收款',
				type: 'bar',
				barWidth: '60%',
				label: {
					normal: {
						show: true,
						position: 'inside'
					}
				},
				data: d.y.slice(0, 10)
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart3.setOption(option);
	};

	return {
		init: init
	};
});